Spring Framework:4.3.14.RELEASE
Framework Modules
创建一个简单的JavaBean:HelloWorld 1 2 3 4 5 6 7 8 9 10 11 12 13 public class HelloWorld { private String name; public void setName(String name) { this.name = name; } public void hello() { System.out.println("Hello " + name); } }
创建一个简单的 applicationContext.xml 1 2 3 4 5 6 7 8 9 10 11 12 13 <?xml version="1.0" encoding="UTF-8" ?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd"> <!--配置 Bean--> <bean id="helloWord" class="com.example.spring.HelloWorld"> <property name="name" value="Example"></property> </bean> </beans>
创建一个简单的使用示例 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 import org.springframework.context.ApplicationContext; import org.springframework.context.support.ClassPathXmlApplicationContext; public class HelloWordTest02 { public static void main(String[] args) { // 创建 Spring IOC 容器 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml"); // 从 IOC 容器中获取 Bean 实例 HelloWorld helloWorld = applicationContext.getBean("helloWorld", HelloWorld.class); // 调用 setName 方法 helloWorld.setName("Example"); // 调用 hello 方法 helloWorld.hello(); } }